var id = getQueryString('id');
var url = basePath+"/admin/addAdmin";

$(function(){
	//管理员类型
	getAdminType();
	
	if(id != null ){
		/*axios.get('/admin/getAdminDetail', {
			params:{
				id:id
			}
		  })
		  .then(function (data) {
			  $('#account').val(data.data.account);
				 $('#name').val(data.data.name);
				 $('#confirmp').val(data.data.password);
				 $('#gender').val(data.data.gender);
				 $("#admin-type").val(data.data.type);
				 var sexHtml="";
				 if(data.data.gender == "0"){
					 //$('.iradio-blue :first').addClass("checked");
					 //$('.iradio-blue :last').removeClass("checked");
					// $('#man').prop("checked",true);
					 sexHtml='<div class="radio-box">'+
												'<input name="gender"  value = "0"  type="radio" id="man"  checked>'+
											   ' <label for="sex-1">男</label>'+
											'</div>'+
											'<div class="radio-box">'+
												'<input type="radio"  value="1" id="woman" name="gender">'+
												'<label for="sex-2">女</label>'+
											'</div>';
					 
				 }else{
					 sexHtml='<div class="radio-box">'+
									'<input name="gender"  value = "0"  type="radio" id="man"  >'+
								   ' <label for="sex-1">男</label>'+
								'</div>'+
								'<div class="radio-box">'+
									'<input type="radio"  value="1" id="woman" name="gender" checked>'+
									'<label for="sex-2">女</label>'+
								'</div>';
					// $('#woman').prop("checked",true);
				 }
				 
				 $('#sex').html(sexHtml);
				 
				// alert(data.data.headportrait);
				 if(data.data.head_portrait != null){
					 $('#picture').attr("src",basePath+'/'+data.data.head_portrait);
				 }
				 
				// $('#listPicture').val(data.data.headportrait);
				 
				 $('#phone').val(data.data.phone);
				 $('#email').val(data.data.email);
				 $('#type').val(data.data.type);
				 $('#description').val(data.data.description);
				 url = basePath+"/admin/editAdmin?id="+id+"";
		  });*/
		
		
		
		
		
		
		
		
		
		
		$.ajax({
			type:"post",
			url:basePath+"/admin/getAdminDetail",
            data:{"id":id},
            async:false,
			success:function(data){
				$('#account').val(data.data.account);
				 $('#name').val(data.data.name);
				 $('#password').val(data.data.password);
				 $('#confirmp').val(data.data.password);
				 $('#gender').val(data.data.gender);
				 $("#admin-type").val(data.data.type);
				 var sexHtml="";
				 if(data.data.gender == "0"){
					 //$('.iradio-blue :first').addClass("checked");
					 //$('.iradio-blue :last').removeClass("checked");
					// $('#man').prop("checked",true);
					 sexHtml='<div class="radio-box">'+
												'<input name="gender"  value = "0"  type="radio" id="man"  checked>'+
											   ' <label for="sex-1">男</label>'+
											'</div>'+
											'<div class="radio-box">'+
												'<input type="radio"  value="1" id="woman" name="gender">'+
												'<label for="sex-2">女</label>'+
											'</div>';
					 
				 }else{
					 sexHtml='<div class="radio-box">'+
									'<input name="gender"  value = "0"  type="radio" id="man"  >'+
								   ' <label for="sex-1">男</label>'+
								'</div>'+
								'<div class="radio-box">'+
									'<input type="radio"  value="1" id="woman" name="gender" checked>'+
									'<label for="sex-2">女</label>'+
								'</div>';
					// $('#woman').prop("checked",true);
				 }
				 
				 $('#sex').html(sexHtml);
				 
				// alert(data.data.headportrait);
				 if(data.data.head_portrait != null){
					 $('#picture').attr("src",basePath+'/'+data.data.head_portrait);
				 }
				 
				// $('#listPicture').val(data.data.headportrait);
				 
				 $('#phone').val(data.data.phone);
				 $('#email').val(data.data.email);
				 $('#type').val(data.data.type);
				 $('#description').val(data.data.description);
				 url = basePath+"/admin/editAdmin?id="+id+"";
				
			}
		});
		
	}

	$('.skin-minimal input').iCheck({
		checkboxClass: 'icheckbox-blue',
		radioClass: 'iradio-blue',
		increaseArea: '20%'
	});
	
	initFileUpload();
	
	
	submitForm();
	
});


//获取管理员类型
var getAdminType = function(){
	$.ajax({
		type:"post",
		url:basePath+"/admin/getRoleList",
		async:false,
		success:function(resp){
			for(var i = 0; i < resp.data.length; i++){
				$("#admin-type").append("<option value='"+resp.data[i].id+"'>"+resp.data[i].role_name+"</option>");
			}
			
		}
	}); 
}

$.validator.addMethod("validateAccount",function(value,element, param){
	var val = false;
	$.ajax({
		type:"post",
		url:basePath+"/admin/validateAccount",
		data:{"id":id,"account":value},
		async:false,
		success:function(data){
			
			if(data.code == "0"){
				val = true;
			}
			
		}
	}); 
    return val; 
},"该帐号已被占用");

//字符验证
$.validator.addMethod("checkAccount",function(value,element, param){
	var checkStr =  /^\w{2,10}$/g;
	return this.optional(element) || (checkStr.test(value)); 
},"帐号必需为2-10位英文字母、数字或者下画线！");

var submitForm = function(){
	$("#form").validate({
		rules:{
			account:{
				required:true,
				checkAccount:true,
				validateAccount:true,
				maxlength:16
			},
			name:{
				required:true,
				maxlength:16
			},
			gender:{
				required:true,
			},
			phone:{
				isPhone:true,
			},
			email:{
				email:true,
			},
		},
		onkeyup:false,
		focusCleanup:true,
		success:"valid",
		submitHandler:function(form){
			$(form).ajaxSubmit({
		        type:"post",
		        url:url,
		        //beforeSubmit: showRequest,
		        success: function(data){
		        	if(data.code == "-3"){
		        		forwardLogin();
			        	 return false;
		        	}
		        	 layer.msg(data.msg,{time:1000});
		        	 setTimeout(function(){
		        		 parent.location.reload();
		        		 var index = parent.layer.getFrameIndex(window.name);
			        	 parent.layer.close(index);
		        	 },2000)
		        	 
		        	
		        }
		      });
		}
	});
}




//编辑时，初始化上传图片的插件，显示移除、修改按钮，隐藏选择图片按钮
var initFileUpload = function(){
	//获取图片的地址
	var picUrl = $("#picture").attr("src");
	
	//如果存在图片，则进行移除、修改的显示，选择图片的隐藏
	if("" != picUrl){
		//预览图片的显示
		$(".fileupload-preview").show();
		//移除、修改的显示，选择图片的隐藏
		$(".fileupload").addClass('fileupload-exists').removeClass('fileupload-new');
	}
	
	
}